package com.shop.service.Impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.DtsAdMapper;
import com.shop.pojo.DtsAd;
import com.shop.service.DtsAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName DtsAdServiceImpl
 * @Author xiao宋
 * @Date 12:39  2022/10/22
 * @Version 1.0
 * @Description TODO
 */
@Service
public class DtsAdServiceImpl implements DtsAdService {
    @Autowired
    DtsAdMapper adMapper;

    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, String content, String name) {
        IPage<DtsAd> iPage = new Page<>(page, limit);
        QueryWrapper<DtsAd> w = new QueryWrapper<>();
        w.eq("deleted", false);
//        前端传过来的广告标题 name
        if (!StringUtils.isEmpty(name)) {
            w.eq("name", name);
        }
//        广告内容
        if (!StringUtils.isEmpty(content)) {
            w.like("content", content);
        }
        //        排序方式
        if ("desc".equalsIgnoreCase(order)) {
            w.orderByDesc(sort);
        } else {
            w.orderByAsc(sort);
        }
        adMapper.selectPage(iPage, w);

        Map<String, Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return data;
    }

    @Override
    public DtsAd creatAD(DtsAd ad) {
        ad.setAddTime(new Date());
        ad.setDeleted(false);
        ad.setUpdateTime(new Date());
        adMapper.insert(ad);
        return ad;
    }

    @Override
    public void updateAD(DtsAd ad) {
        ad.setUpdateTime(new Date());
        adMapper.updateById(ad);
    }

    @Override
    public void deleteAD(DtsAd ad) {
        ad.setUpdateTime(new Date());
        ad.setDeleted(true);
        adMapper.updateById(ad);
    }
}
